﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBAccessLib;
using LineWatch.Common;
using System.Configuration;
using System.Windows.Forms;

namespace LineWatch_WinForm
{
    public class ConfigInfo
    {
        public static int CurrentLineCodeCount = 1;
        public static float ProductFontSizeTitle = 60;
        public static float ProductFontSizeCaption = 72;
        public static float ProductFontSizeValue = 70;
        public static float PositionFontSizeValue1 = 60;
        public static float PositionFontSizeValue2 = 85;
        public static float CommentFontSizeTitle = 100;
        public static float CommentFontSizeCaption = 32;
        public static float CommentFontSizeValue = 30;
        public static DataTable DtAllLines = null;
        public static DataTable DtAllPositions = null;
        public static DataTable DtLineWatchSys1 = null;
        
        public static string LineCode1 = string.Empty;

        public static string ShiftCodeA = "ShiftA";
        public static string ShiftCodeB = "ShiftB";
        public static string ShiftCodeC = "ShiftC";
        public static string ShiftCodeZ = "ShiftZ";

        public static int PositionNumInOneCommand = 20;

        public static string PlanNumSettingCommand = string.Empty;
        public static string CTTimeSettingCommand = string.Empty;

        public static string PlanNumResetCommand = string.Empty;
        public static string NowPlanNumResetCommand = string.Empty;
        public static string ProductNumResetCommand = string.Empty;
        public static string StopTimeResetCommand = string.Empty;

        public static string StopTime = "0";
        public static string ActualCompleted = "0";
        public static string NowPlan = "0";

        public static string LastShiftTime = "";
        public static string LastNowPlan = "0";
        
        /// <summary>
        /// Init Config Info
        /// </summary>
        public static void InitConfigInfo()
        {
            float tempSize = 0F;

            if (float.TryParse(ConfigurationManager.AppSettings["ProductFontSizeTitle"].ToString().Trim(), out tempSize))
            {
                ProductFontSizeTitle = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["ProductFontSizeCaption"].ToString().Trim(), out tempSize))
            {
                ProductFontSizeCaption = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["ProductFontSizeValue"].ToString().Trim(), out tempSize))
            {
                ProductFontSizeValue = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["PositionFontSizeValue1"].ToString().Trim(), out tempSize))
            {
                PositionFontSizeValue1 = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["PositionFontSizeValue2"].ToString().Trim(), out tempSize))
            {
                PositionFontSizeValue2 = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["CommentFontSizeTitle"].ToString().Trim(), out tempSize))
            {
                CommentFontSizeTitle = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["CommentFontSizeCaption"].ToString().Trim(), out tempSize))
            {
                CommentFontSizeCaption = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["CommentFontSizeValue"].ToString().Trim(), out tempSize))
            {
                CommentFontSizeValue = tempSize;
            }

            LastShiftTime = ConfigurationManager.AppSettings["LastShiftTime"].ToString().Trim();
            LastNowPlan = ConfigurationManager.AppSettings["LastNowPlan"].ToString().Trim();
        }

        /// <summary>
        /// Get Line Name By Code
        /// </summary>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static string GetLineNameByCode(string lineCode)
        {
            foreach (DataRow row in ConfigInfo.DtAllLines.Rows)
            {
                if (string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                {
                    return row["Line_Name"].ToString().Trim();
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// Get Line Serial Port By Code
        /// </summary>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static string GetLineSerialPortByCode(string lineCode)
        {
            foreach (DataRow row in ConfigInfo.DtAllLines.Rows)
            {
                if (string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                {
                    return row["Serial_Port"].ToString().Trim();
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// Get Shift
        /// </summary>
        /// <param name="lineShift"></param>
        /// <returns></returns>
        public static string GetShift(DataTable lineShift)
        {
            string shift = string.Empty;
            DateTime shiftAStartTime = ConfigInfo.GetShitAStartTime(lineShift);
            DateTime shiftAEndTime = ConfigInfo.GetShitAEndTime(lineShift);
            DateTime shiftBStartTime = ConfigInfo.GetShitBStartTime(lineShift);
            DateTime shiftBEndTime = ConfigInfo.GetShitBEndTime(lineShift);
            DateTime shiftCStartTime = ConfigInfo.GetShitCStartTime(lineShift);
            DateTime shiftCEndTime = ConfigInfo.GetShitCEndTime(lineShift);
            DateTime currentTime = ConfigInfo.GetCurrentTime();

            if ((currentTime >= shiftAStartTime)
                && (currentTime <= shiftAEndTime))
            {
                shift = ConfigInfo.ShiftCodeA;
            }
            else if ((currentTime >= shiftBStartTime)
                && (currentTime <= shiftBEndTime))
            {
                shift = ConfigInfo.ShiftCodeB;
            }
            else if ((currentTime >= shiftCStartTime)
                && (currentTime <= shiftCEndTime))
            {
                shift = ConfigInfo.ShiftCodeC;
            }
            else
            {
                shift = ConfigInfo.ShiftCodeZ;
            }

            return shift;
        }

        /// <summary>
        /// Get Shift Info
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetShiftInfo(DataTable lineWatchSys)
        {
            string shiftInfo = string.Empty;
            string shift = ConfigInfo.GetShift(lineWatchSys);

            if (string.Equals(shift, ConfigInfo.ShiftCodeA))
            {
                shiftInfo = "班次：1班";
            }
            else if (string.Equals(shift, ConfigInfo.ShiftCodeB))
            {
                shiftInfo = "班次：2班";
            }
            else if (string.Equals(shift, ConfigInfo.ShiftCodeC))
            {
                shiftInfo = "班次：3班";
            }
            else
            {
                shiftInfo = "班次：未知";
            }

            return shiftInfo;
        }

        /// <summary>
        /// Get Now Plan Info
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <param name="planNum"></param>
        /// <returns></returns>
        public static string GetNowPlanInfo(DataTable lineShift)
        {
            string nowPlan = "0";
            DateTime shiftAStartTime = ConfigInfo.GetShitAStartTime(lineShift);
            DateTime shiftAEndTime = ConfigInfo.GetShitAEndTime(lineShift);
            DateTime shiftBStartTime = ConfigInfo.GetShitBStartTime(lineShift);
            DateTime shiftBEndTime = ConfigInfo.GetShitBEndTime(lineShift);
            DateTime shiftCStartTime = ConfigInfo.GetShitCStartTime(lineShift);
            DateTime shiftCEndTime = ConfigInfo.GetShitCEndTime(lineShift);
            string currentShift = ConfigInfo.GetShift(lineShift);

            DateTime lastShiftTime = DateTime.Now;
            string lastShift = string.Empty;

            if (DateTime.TryParse(ConfigInfo.LastShiftTime, out lastShiftTime))
            {
                if ((lastShiftTime >= shiftAStartTime)
                    && (lastShiftTime <= shiftAEndTime))
                {
                    lastShift = ConfigInfo.ShiftCodeA;
                }
                else if ((lastShiftTime >= shiftBStartTime)
                    && (lastShiftTime <= shiftBEndTime))
                {
                    lastShift = ConfigInfo.ShiftCodeB;
                }
                else if ((lastShiftTime >= shiftCStartTime)
                    && (lastShiftTime <= shiftCEndTime))
                {
                    lastShift = ConfigInfo.ShiftCodeC;
                }
                else
                {
                    lastShift = "Expired";
                }

                if (string.Equals(currentShift, lastShift))
                {
                    int temp = 0;

                    if (int.TryParse(ConfigInfo.LastNowPlan, out temp))
                    {
                        nowPlan = ConfigInfo.LastNowPlan;
                    }
                }
            }

            return nowPlan;
        }

        /// <summary>
        /// Get ShitA Start Time
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static DateTime GetShitAStartTime(DataTable lineWatchSys)
        {
            //string strTime = string.Empty;

            //foreach (DataRow row in lineWatchSys.Rows)
            //{
            //    if (string.Equals("ShiftA", row["Function"].ToString().Trim()))
            //    {
            //        strTime = row["Content1"].ToString().Trim();
            //        break;
            //    }
            //}

            //string today = DateTime.Today.ToString("yyyy-MM-dd");
            //string dayAndTime = today + " " + strTime;
            //DateTime dateTime = DateTime.Parse(dayAndTime);

            //return dateTime;


            string strTime = "00:00";

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ShiftA", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content1"].ToString().Trim();
                    break;
                }
            }

            string today = GetCurrentWorkDate(lineWatchSys).ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime + ":00";
            DateTime dateTime = DateTime.Parse(dayAndTime);

            return dateTime;
        }

        /// <summary>
        /// Get ShitA End Time
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static DateTime GetShitAEndTime(DataTable lineWatchSys)
        {
            //string strTime = string.Empty;

            //foreach (DataRow row in lineWatchSys.Rows)
            //{
            //    if (string.Equals("ShiftA", row["Function"].ToString().Trim()))
            //    {
            //        strTime = row["Content2"].ToString().Trim();
            //        break;
            //    }
            //}

            //string today = DateTime.Today.ToString("yyyy-MM-dd");
            //string dayAndTime = today + " " + strTime;
            //DateTime dateTime = DateTime.Parse(dayAndTime);

            //return dateTime;


            string strTime = "00:00";

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ShiftA", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content2"].ToString().Trim();
                    break;
                }
            }

            string today = GetCurrentWorkDate(lineWatchSys).ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime + ":59";
            DateTime dateTime = DateTime.Parse(dayAndTime);

            DateTime shiftAStartTime = ConfigInfo.GetShitAStartTime(lineWatchSys);

            if (dateTime <= shiftAStartTime)
            {
                dateTime = dateTime.AddDays(1);
            }

            return dateTime;
        }

        /// <summary>
        /// Get ShitB Start Time
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static DateTime GetShitBStartTime(DataTable lineWatchSys)
        {
            //string strTime = string.Empty;

            //foreach (DataRow row in lineWatchSys.Rows)
            //{
            //    if (string.Equals("ShiftB", row["Function"].ToString().Trim()))
            //    {
            //        strTime = row["Content1"].ToString().Trim();
            //        break;
            //    }
            //}

            //string today = DateTime.Today.ToString("yyyy-MM-dd");
            //string dayAndTime = today + " " + strTime;
            //DateTime dateTime = DateTime.Parse(dayAndTime);

            //return dateTime;


            string strTime = "00:00";

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ShiftB", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content1"].ToString().Trim();
                    break;
                }
            }

            string today = GetCurrentWorkDate(lineWatchSys).ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime + ":00";
            DateTime dateTime = DateTime.Parse(dayAndTime);

            return dateTime;
        }

        /// <summary>
        /// Get ShitB End Time
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static DateTime GetShitBEndTime(DataTable lineWatchSys)
        {
            //string strTime = string.Empty;

            //foreach (DataRow row in lineWatchSys.Rows)
            //{
            //    if (string.Equals("ShiftB", row["Function"].ToString().Trim()))
            //    {
            //        strTime = row["Content2"].ToString().Trim();
            //        break;
            //    }
            //}

            //string today = DateTime.Today.AddDays(1).ToString("yyyy-MM-dd");
            //string dayAndTime = today + " " + strTime;
            //DateTime dateTime = DateTime.Parse(dayAndTime);

            //return dateTime;


            string strTime = "00:00";

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ShiftB", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content2"].ToString().Trim();
                    break;
                }
            }

            string today = GetCurrentWorkDate(lineWatchSys).ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime + ":59";
            DateTime dateTime = DateTime.Parse(dayAndTime);

            DateTime shiftBStartTime = ConfigInfo.GetShitBStartTime(lineWatchSys);

            if (dateTime <= shiftBStartTime)
            {
                dateTime = dateTime.AddDays(1);
            }

            return dateTime;
        }

        /// <summary>
        /// Get ShitC Start Time
        /// </summary>
        /// <param name="lineShift"></param>
        /// <returns></returns>
        public static DateTime GetShitCStartTime(DataTable lineShift)
        {
            string strTime = "00:00";

            foreach (DataRow row in lineShift.Rows)
            {
                if (string.Equals("ShiftC", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content1"].ToString().Trim();
                    break;
                }
            }

            string today = GetCurrentWorkDate(lineShift).ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime + ":00";
            DateTime dateTime = DateTime.Parse(dayAndTime);

            return dateTime;
        }

        /// <summary>
        /// Get ShitC End Time
        /// </summary>
        /// <param name="lineShift"></param>
        /// <returns></returns>
        public static DateTime GetShitCEndTime(DataTable lineShift)
        {
            string strTime = "00:00";

            foreach (DataRow row in lineShift.Rows)
            {
                if (string.Equals("ShiftC", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content2"].ToString().Trim();
                    break;
                }
            }

            string today = GetCurrentWorkDate(lineShift).ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime + ":59";
            DateTime dateTime = DateTime.Parse(dayAndTime);

            DateTime shiftCStartTime = ConfigInfo.GetShitCStartTime(lineShift);

            if (dateTime <= shiftCStartTime)
            {
                dateTime = dateTime.AddDays(1);
            }

            return dateTime;
        }

        /// <summary>
        /// Get Work Day Start Time
        /// </summary>
        /// <param name="lineShift"></param>
        /// <returns></returns>
        public static DateTime GetWorkDayStartTime(DataTable lineShift)
        {
            string strTime = "00:00";

            foreach (DataRow row in lineShift.Rows)
            {
                if (string.Equals("ShiftA", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content1"].ToString().Trim();
                    break;
                }
            }

            string today = DateTime.Today.ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime + ":00";
            DateTime dateTime = DateTime.Parse(dayAndTime);

            return dateTime;
        }

        /// <summary>
        /// Get Current Work Date
        /// </summary>
        /// <param name="lineShift"></param>
        /// <returns></returns>
        public static DateTime GetCurrentWorkDate(DataTable lineShift)
        {
            string currentWorkDate = string.Empty;
            DateTime shiftAStartTime = ConfigInfo.GetWorkDayStartTime(lineShift);

            DateTime currentTime = ConfigInfo.GetCurrentTime();

            if (currentTime >= shiftAStartTime)
            {
                currentWorkDate = currentTime.ToString("yyyy-MM-dd");
            }
            else
            {
                currentWorkDate = currentTime.AddDays(-1).ToString("yyyy-MM-dd");
            }

            return DateTime.Parse(currentWorkDate);
        }

        /// <summary>
        /// Get Current Time
        /// </summary>
        /// <returns></returns>
        public static DateTime GetCurrentTime()
        {
            string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            return DateTime.Parse(currentTime);
        }

        /// <summary>
        /// Init All Positions
        /// </summary>
        public static void InitAllPositions()
        {
            LinePositionModel linePositionModel = Position.GetAllLinePositions();
            DtAllPositions = linePositionModel.LinePositionTable;
        }

        /// <summary>
        /// Get Min Position
        /// </summary>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static int GetMinPosition(string lineCode)
        {
            int minDMPosition = 99999999;

            if (ConfigInfo.DtAllPositions != null)
            {
                foreach (DataRow row in ConfigInfo.DtAllPositions.Rows)
                {
                    int dmPosition = 0;

                    if (string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                    {
                        if (int.TryParse(row["Content1"].ToString().Trim(), out dmPosition))
                        {
                            if (minDMPosition > dmPosition)
                            {
                                minDMPosition = dmPosition;
                            }
                        }
                    }
                }
            }

            return minDMPosition;
        }

        /// <summary>
        /// Get Max Position
        /// </summary>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static int GetMaxPosition(string lineCode)
        {
            int maxDMPosition = 0;

            if (ConfigInfo.DtAllPositions != null)
            {
                foreach (DataRow row in ConfigInfo.DtAllPositions.Rows)
                {
                    int dmPosition = 0;

                    if (string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                    {
                        if (int.TryParse(row["Content1"].ToString().Trim(), out dmPosition))
                        {
                            if (maxDMPosition < dmPosition)
                            {
                                maxDMPosition = dmPosition;
                            }
                        }
                    }
                }
            }

            return maxDMPosition;
        }

        /// <summary>
        /// Get Alarm Music Name
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetAlarmMusicName(DataTable lineWatchSys)
        {
            string alarmMusicName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("MusicA", row["Function"].ToString().Trim()))
                {
                    alarmMusicName = row["Content1"].ToString().Trim();
                    break;
                }
            }

            return alarmMusicName;
        }

        /// <summary>
        /// Get Help Music Name
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetHelpMusicName(DataTable lineWatchSys)
        {
            string helpMusicName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("MusicH", row["Function"].ToString().Trim()))
                {
                    helpMusicName = row["Content1"].ToString().Trim();
                    break;
                }
            }

            return helpMusicName;
        }

        /// <summary>
        /// Get Stop Music Name
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetStopMusicName(DataTable lineWatchSys)
        {
            string stopMusicName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("MusicS", row["Function"].ToString().Trim()))
                {
                    stopMusicName = row["Content1"].ToString().Trim();
                    break;
                }
            }

            return stopMusicName;
        }

        /// <summary>
        /// Get Screen Time Span 1
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static int GetScreenTimeSpan1(DataTable lineWatchSys)
        {
            string screenTimeSpan = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenT", row["Function"].ToString().Trim()))
                {
                    screenTimeSpan = row["Content1"].ToString().Trim();
                    break;
                }
            }

            int temp = 0;
            if (int.TryParse(screenTimeSpan, out temp))
            {
                return int.Parse(screenTimeSpan);
            }

            return 0;
        }

        /// <summary>
        /// Get Screen Time Span 2
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static int GetScreenTimeSpan2(DataTable lineWatchSys)
        {
            string screenTimeSpan = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenT", row["Function"].ToString().Trim()))
                {
                    screenTimeSpan = row["Content2"].ToString().Trim();
                    break;
                }
            }

            int temp = 0;
            if (int.TryParse(screenTimeSpan, out temp))
            {
                return int.Parse(screenTimeSpan);
            }

            return 0;
        }

        /// <summary>
        /// Get Screen Picture Name 1
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetScreenPictureName1(DataTable lineWatchSys)
        {
            string screenPictureName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenP", row["Function"].ToString().Trim()))
                {
                    screenPictureName = row["Content1"].ToString().Trim();
                    break;
                }
            }

            return screenPictureName;
        }

        /// <summary>
        /// Get Screen Picture Name 2
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetScreenPictureName2(DataTable lineWatchSys)
        {
            string screenPictureName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenP", row["Function"].ToString().Trim()))
                {
                    screenPictureName = row["Content2"].ToString().Trim();
                    break;
                }
            }

            return screenPictureName;
        }

        /// <summary>
        /// Get Screen Picture Name 3
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetScreenPictureName3(DataTable lineWatchSys)
        {
            string screenPictureName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenP", row["Function"].ToString().Trim()))
                {
                    screenPictureName = row["Content3"].ToString().Trim();
                    break;
                }
            }

            return screenPictureName;
        }

        /// <summary>
        /// Get Screen Picture Name 4
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetScreenPictureName4(DataTable lineWatchSys)
        {
            string screenPictureName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenP", row["Function"].ToString().Trim()))
                {
                    screenPictureName = row["Content4"].ToString().Trim();
                    break;
                }
            }

            return screenPictureName;
        }

        /// <summary>
        /// Save Now Plan Value
        /// </summary>
        public static void SaveNowPlanValue()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            config.AppSettings.Settings["LastShiftTime"].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            config.AppSettings.Settings["LastNowPlan"].Value = ConfigInfo.NowPlan;

            config.Save();
            ConfigurationManager.RefreshSection("appSettings");
            config = null;
        }
    }
}